﻿using MediatR;
using Process.API.Application.Command.Action.ProductionOrders;
using Process.Domain;
using Process.ErrorCode;
using Process.Infrastructure.Implement;
using Process.Infrastructure.Interface;

namespace Process.API.Application.Handler.Action.ProductionOrdersAction
{
    public class DelProductionOrdersHandler: IRequestHandler<DelProductionOrdersCommand ,APIResult<int>>
    {
        DatabaseContext db;
        IBaseRepository<ProductionOrders> proRep;

        public DelProductionOrdersHandler(DatabaseContext db, IBaseRepository<ProductionOrders> proRep)
        {
            this.db = db;
            this.proRep = proRep;
        }

        public async Task<APIResult<int>> Handle(DelProductionOrdersCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var m = proRep.GetAll().FirstOrDefault(a => a.ProductionOrdersId == request.ProductionOrdersId);
            m.IsDel = true;
            var ml = await db.SqlSugar.Updateable(m).ExecuteCommandAsync();
            result.Code = APIEnum.Ok;
            result.Message = "生产工单删除成功";
            result.Data = ml;
            return await Task.FromResult(result);
        }
    }
}
